package com.yuyou.fn.tradingservice.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.tradingservice.vo.ServiceCategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuyou.fn.tradingservice.mapper.ServiceCategoryMapper;
import com.yuyou.fn.tradingservice.entity.ServiceCategory;
import com.yuyou.fn.tradingservice.service.IServiceCategoryService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * ServiceCategory 表数据服务层接口实现类
 *
 */
@Service
public class ServiceCategoryServiceImpl extends SuperServiceImpl<ServiceCategoryMapper, ServiceCategory,Long> implements IServiceCategoryService {

    @Override
    public void saveCategory(ServiceCategoryVo categoryVo) {
        ServiceCategory category = new ServiceCategory();
        category.setAttribute1(categoryVo.getAttribute1());
        category.setAttribute2(categoryVo.getAttribute2());
        category.setAttribute3(categoryVo.getAttribute3());
        category.setAttribute4(categoryVo.getAttribute4());
        category.setAttribute5(categoryVo.getAttribute5());
        if(categoryVo.getCategoryId() != null) {
            category.setParentId(categoryVo.getCategoryId());
            category.setType(this.findById(categoryVo.getCategoryId()).getType());
        }
        category.setName(categoryVo.getName());
        category.setParentId(0L);
        category.setCreateTime(TimeUtils.now());
        category.setUpdateTime(TimeUtils.now());
        insertSelective(category);
    }

    @Override
    public void editCategory(ServiceCategoryVo categoryVo) {
        if(categoryVo.getCategoryId().equals(categoryVo.getParentId())) {
            categoryVo.setParentId(0L);
        }
        ServiceCategory category = new ServiceCategory();
        BeanUtils.copyProperties(category, categoryVo);
        category.setUpdateTime(TimeUtils.now());
        updateSelectiveById(category);
    }

    @Override
    public Page<ServiceCategory> findCategoryForPage(ServiceCategoryVo category, int pageNo, int pageSize) {
        Example<ServiceCategory> example = ServiceCategory.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria, category);
        return findPageByExample(example, pageNo, pageSize);
    }

    @Override
    public String findCategoryNameById(Long categoryId) {
        Example<ServiceCategory> example = ServiceCategory.createExample();
        example.selectProperties("name");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryId",categoryId);
        ServiceCategory category = findOneByExample(example);
        return category == null ? StringUtils.EMPTY : category.getName() ;
    }

    @Override
    public List<ServiceCategory> getChildCategory(Long categoryId) {
        Example<ServiceCategory> example = ServiceCategory.createExample();
        example.createCriteria().andEqualTo("parentId", categoryId);
        return this.findListByExample(example);
    }

    @Override
    public List<ServiceCategory> findCategoryByType(Integer type) {
        Example<ServiceCategory> example = ServiceCategory.createExample();
        example.createCriteria().andEqualTo("type", type);
        return this.findListByExample(example);
    }

    @Override
    public List<Long> getAllChildCategory(Long categoryId) {
        List<Long> categoryIds = new ArrayList<>();
        List<ServiceCategory> categoryList = this.findCategoryByType(this.findById(categoryId).getType());
        return this.getCategoryIds(categoryList, categoryId, categoryIds);
    }

    public List<Long> getCategoryIds(List<ServiceCategory> categoryList, Long pid, List<Long> categoryIds){
        for(ServiceCategory category: categoryList){
            if(category.getParentId() != null) {
                if(category.getParentId().equals(pid)){
                    getCategoryIds(categoryList,category.getCategoryId(), categoryIds);
                    categoryIds.add(category.getCategoryId());
                }
            }
        }
        return categoryIds;
    }

}